package com.egu.web.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.egu.common.util.UserUtil;
import com.egu.model.User;
import com.egu.service.UserService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Controller
@RequestMapping("user")
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("listUI")
	public String listUI() {
		return "user/listUI";
	}

	@RequestMapping("userCenter")
	public String userCenter() {
		return "user/userCenter";
	}
	@RequestMapping("resetPw")
	public String resetPw() {
		return "user/resetPw";
	}
	
	@RequestMapping("savePw")
	public String savePw(String userPw,String newUserPw,Model model) {
		boolean flag=userService.savePw(userPw,newUserPw);
		if(flag==true) {
			model.addAttribute("messages","重置密码成功!");
		}else {
			model.addAttribute("messages","重置密码失败!");	
		}
		return "user/resetPwCallback";
	}
	
	
	
	
	@RequestMapping("checkUserNumIsExist")
	@ResponseBody
	public RequestResultVo<User> checkUserNumIsExist(String userNum) {
		RequestResultVo<User> resultVo = new RequestResultVo<>();
		User user = this.userService.findUserByUserName(userNum);
		if (user == null) {
			return resultVo;
		}else {
			resultVo.setSuccess(false);
			resultVo.setErrorMessage("该账号已经存在");
			return resultVo;
		}
	}
	
	
	
	@RequestMapping("/getCurrentUser")
	@ResponseBody
	public User getCurrentUser() {
		User user = UserUtil.getCurrentUser();
		return user;
	}

	@RequestMapping("/getAllList")
	@ResponseBody
	public RequestResultVo<List<User>> getAllList() {
		RequestResultVo<List<User>> vo = userService.getAllList();
		return vo;
	}

	@RequestMapping("/getList")
	@ResponseBody
	public RequestPageVo<User> getList(@ModelAttribute(value = "param") PageParam param, User user) {
		RequestPageVo<User> vo = userService.getList(param, user);
		return vo;
	}

	@RequestMapping("/saveUser")
	public String saveUser(User user,Model model) {
		RequestResultVo<User> result = new RequestResultVo<User>();
		if (user != null) {
			userService.saveOrUpdate(user);
			model.addAttribute("messages", "用户资料更改成功!");
		} else {
			result.setSuccess(false);
			model.addAttribute("messages", "用户资料更改失败!");
		}
		return "user/saveUserCallback";
	}
	
	
	@RequestMapping("/saveOrUpdate")
	@ResponseBody
	public RequestResultVo<User> saveOrUpdate(User user) {
		RequestResultVo<User> result = new RequestResultVo<User>();
		if (user != null) {
			return userService.saveOrUpdate(user);
		} else {
			result.setSuccess(false);
		}
		return result;
	}

	@RequestMapping("/deleteDataById")
	public @ResponseBody RequestResultVo<User> deleteDataById(
			@RequestParam(value = "id", required = false) Integer id) {
		RequestResultVo<User> result = new RequestResultVo<User>();
		if (id == null) {
			result.setSuccess(false);
			result.setErrorMessage("id为空删除数据失败");
			return result;
		}
		result = userService.deleteDataById(id);
		return result;
	}

	
	@RequestMapping("/uploadTouXiang")
	@ResponseBody
	public RequestResultVo<String> uploadTouXiang(MultipartFile touxiang,HttpServletRequest request) throws IOException {
		RequestResultVo<String> resultVo = new RequestResultVo<String>();
		
		InputStream inputStream = touxiang.getInputStream();
		
		String path="/statics/images";
		String realPath=getRealPath(path,request);
		String photoUrl=getNewImgName(touxiang);
		String finalPath=realPath+"\\"+photoUrl;
		
		String url=path+"/"+photoUrl;
		//保存到数据库
		User currentUser = UserUtil.getCurrentUser();
		currentUser.setPhotoUrl(url);
		userService.saveOrUpdate(currentUser);
		
		FileOutputStream fileOutputStream = new FileOutputStream(finalPath);
		IOUtils.copy(inputStream, fileOutputStream);
		
		
		//resultVo.setResult(finalPath);
		resultVo.setResult(url);
		return resultVo;
	}
	
	public String getRealPath(String path, HttpServletRequest request) {// 项目中的上传路径/img
		// 需要request,调用方法,得到真实对象
		String realPath = request.getServletContext().getRealPath(path);
		// 根据路径包装一个file对象
		File file = new File(realPath);
		// 通过file对象检查文件夹是否被创建
		if (!file.exists()) {
			file.mkdirs();// 创建目录
		}
		return realPath;
	}
	public String getNewImgName(MultipartFile file) {
		// 得到文件原名称
		String originalFilename = file.getOriginalFilename();// aaa.jpg
		// 得到后缀
		int lastIndexOf = originalFilename.lastIndexOf(".");
		String newName = originalFilename.substring(lastIndexOf);// .jpg
		// 生成uuid
		String uuid = UUID.randomUUID().toString().substring(0, 6);
		// 返回新名称
		return uuid + newName;
	}
}
